package com.jicheng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jicheng.Utils.ResultMess;
import com.jicheng.dto.app.*;
import com.jicheng.ext.dto.PagingResult;
import com.jicheng.service.IAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(tags = "app-manager(app更新管理)")
@RestController
@RequestMapping(value = "/app-manager", produces = "application/json;charset=UTF-8")
public class AppUpdateController {

    @Autowired
    private IAppService appService;

    /**
     * 用与后台上传app时候，把app信息存到数据库
     *
     * @param params
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存版本")
    public void save(@RequestBody AppParams params) {
        appService.saveVersion(params);
    }

    /**
     * 查看历史版本
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "查看历史版本-版本发布记录")
    public PagingResult<AppDto> getList(AppPagingParams params) {
        return appService.getList(params);
    }

    /**
     * 获取最新版本
     *
     * @return
     */
    @RequestMapping(value = "/last", method = RequestMethod.GET)
    @ApiOperation(value = "获取最新版本")
    public ResultMess<Map> getLastVersion() {
        return new ResultMess<>(true,"",appService.getLastVersion());
    }

    /**
     * 用户每更新一次app,都网数据库记录
     *
     * @param appHistoryParams
     */
    @RequestMapping(value = "/saveAppHistory", method = RequestMethod.POST)
    @ApiOperation(value = "保存终端用户更新app记录")
    public void saveAppHistory(@RequestBody AppHistoryParams appHistoryParams) {
        appService.saveAppHistory(appHistoryParams);
    }


    /**
     * 终端系统更新列表
     *
     * @param appPagingParams
     * @return
     */
    @RequestMapping(value = "/getAppUpdateList", method = RequestMethod.GET)
    @ApiOperation(value = "终端系统更新列表")
    public ResultMess<AppUpdateDto> getAppUpdateList(AppPagingParams appPagingParams) {
        PageHelper.startPage(appPagingParams.getPage(), appPagingParams.getPageSize());
        List<AppUpdateDto> list = appService.getAppUpdateList(appPagingParams);
        PageInfo pageInfo = new PageInfo<>(list);
        PagingResult<AppUpdateDto> result = new PagingResult();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return new ResultMess(true,"",result);
    }

    /**
     * 查看终端历史更新情况
     *
     * @param appId
     * @param page
     * @param pageSize
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/getAppHistoryList", method = RequestMethod.GET)
    @ApiOperation(value = "查看终端历史更新情况")
    public ResultMess<AppHistoryDto> getAppHistoryList(String appId, int page, int pageSize, String startTime, String endTime) {
        PageHelper.startPage(page, pageSize);
        List<AppHistoryDto> list = appService.getAppHistoryList(appId, startTime, endTime);
        PageInfo pageInfo = new PageInfo<>(list);
        PagingResult<AppHistoryDto> result = new PagingResult();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return  new ResultMess(true,"",result);
    }

    @RequestMapping(value = "/addOrUpdateMonitoring", method = RequestMethod.POST)
    @ApiOperation(value = "添加或编辑监督清单")
    public ResultMess addMonitoringList(@RequestBody MonitoringList params) {

        if(params.getId().isEmpty()){

            try {
                appService.addMonitoringList(params);
                return new ResultMess(true, "添加成功！");
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultMess(false, "添加失败！");
            }
        }else{
            try {
                appService.updateMonitoringList(params);
                return new ResultMess(true, "编辑成功！");
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultMess(false, "编辑失败！");
            }
        }
    }


    @RequestMapping(value = "/getMonitoringList", method = RequestMethod.GET)
    @ApiOperation(value = "获取监督清单列表")
    public ResultMess<MonitoringList> getMonitoringList(MonitoringPagingParams params) {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<MonitoringList> list = appService.getMonitoringList(params);
        PageInfo pageInfo = new PageInfo<>(list);
        PagingResult<MonitoringList> result = new PagingResult();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());

        return new ResultMess(true,"",result);
    }

    @RequestMapping(value = "/delMonitoringList", method = RequestMethod.GET)
    @ApiOperation(value = "删除监督清单")
    public ResultMess delMonitoringList(String id) {
        try {
            appService.delMonitoringList(id);
            return new ResultMess(true, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "删除失败！");
        }
    }
    @RequestMapping(value = "/checkMonitoringList", method = RequestMethod.GET)
    @ApiOperation(value = "查看监督清单")
    public ResultMess<MonitoringList> checkMonitoringList(String id) {
        MonitoringList monitoringList =  appService.checkMonitoringList(id);
            return new ResultMess(true, "",monitoringList);
    }

    @RequestMapping(value = "/getConditions", method = RequestMethod.GET)
    @ApiOperation(value = "获取监督清单下拉条件查询")
    public ResultMess<Map<String,List<String>>> getConditions() {
        Map<String,List<String>> conditions =  appService.getConditions();
        return new ResultMess(true, "",conditions);
    }
}
